var UserOrder = app.controller('UserOrder', function($rootScope, $scope, $http,$state,$stateParams,$interval,UserOrderData,anchorScroll) {
	try{
		if($rootScope.istutor){
			$rootScope.showf=false;
		}else{
			$rootScope.showf=true;
		}
		$scope.show=false;
		$rootScope.tab=3;
		$rootScope.title = '我的订单';

		$scope.busy = false;
		$scope.currentPage = 1;
		$scope.limit = 10;
		$scope.pages = 1;
		$scope.p=0;
		
		var url=SITE_URL+"index.php?app=userapp&callback=JSON_CALLBACK&act=pledge";
		$http.jsonp(url).success(function(data) {
			if(data.done){
				$scope.p=data.retval;
			}
		});
		
		//加载订单列表信息
		$scope.busy = true;
		$scope.order=[];
		$scope.status=$stateParams.status;
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=order&status="+$stateParams.status;
		$http.jsonp(url).success(function(data) {
			$scope.busy = false;
			$scope.order=data.retval.data;
			if($scope.order.length>0){
				for(var i=0;i<$scope.order.length;i++){

					if($scope.order[i].timeready!=undefined &&$scope.order[i].timeready>0){
						$scope.djs(i);						
					}
				}
			}
			$scope.pages = Math.ceil(data.retval.total/$scope.limit);
			
			var id=$stateParams.id;
			if(id>0){
				setTimeout(function(){
					anchorScroll.toView('#'+id, true);
				},500);
			}
		});


		$rootScope.$on('$cordovaChat:ChatMessageReciveOrtherEventUser', function (event, msg) {
			try {
				console.log('$cordovaChat:ChatMessageReciveOrtherEventUser',msg);
				var d = JSON.parse(window.localStorage.user);
				if(msg.touser==d.CHATUSER){
					
					var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=order&status="+$stateParams.status;
					$http.jsonp(url).success(function(data) {
						$scope.order=data.retval.data;
						if($scope.order.length>0){
							for(var i=0;i<$scope.order.length;i++){

								if($scope.order[i].timeready!=undefined &&$scope.order[i].timeready>0){
									$scope.djs(i);						
								}
							}
						}
						$scope.pages = Math.ceil(data.retval.total/$scope.limit);
					});
				}
			} catch (e) {
				showcatch(e);
			}
		});

		var timelist=[];
		$scope.djs=function(i){
			var timer =  $interval(function(){
				var c=$scope.order[i].timeready;
				$scope.order[i].h=parseInt(c/(60*60));
				if($scope.order[i].h<10){
					$scope.order[i].h='0'+$scope.order[i].h;
				}
				$scope.order[i].m=parseInt((c%(60*60))/60);
				if($scope.order[i].m<10){
					$scope.order[i].m='0'+$scope.order[i].m;
				}
				$scope.order[i].s=parseInt((c%(24*60*60))%60);
				if($scope.order[i].s<10){
					$scope.order[i].s='0'+$scope.order[i].s;
				}
				$scope.order[i].timeready--;

				if($scope.order[i].timeready<=0){
					for(var j=0;j<timelist.length;j++){
						if(timelist[j].k==i){
							$interval.cancel(timelist[j].t);
						}
					}

					var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=orderalready&id="+$scope.order[i].consult_tid;
					$http.jsonp(url).success(function(data) {
						if(data.done){
							$scope.order[i].already=data.retval.already;
						}
					});
				}
			},1000);
			timelist.push({"k":i,"t":timer});
		}
		
		//播放视频
		$scope.playvideo = function(path){
			if(path){
				videoplay($rootScope.site+path);
			}
		}
		

		//加载更多
		$scope.loadMore = function() {
			try{
				if ($scope.currentPage < $scope.pages) {

					if ($scope.busy) {
						return false; 
					}
					$scope.busy = true;
					$scope.currentPage++;

					var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=order&status="+$stateParams.status+"&page="+$scope.currentPage;
					$http.jsonp(url).success(function(data) {
						for (var i in data.retval.data) {
							if(typeof data.retval.data[i]!="function")
								$scope.order.push(data.retval.data[i]);
						}
						$scope.pages = Math.ceil(data.retval.total/$scope.limit);

						$scope.busy = false;
						if($scope.pages<=$scope.currentPage){
							//$scope.alerts = [{type : 'success' , msg : '没有更多内容了'}]
						}
					});
				}
			}
			catch (e) {
				showcatch(e);
			}
		};

		$scope.tutorname="";
		$scope.amount=0;
		$scope.s={};
		//预支付
		$scope.prepay=function(s){
			try{
				$scope.s=s;
				var n=s.t;
				var o=s.$parent.order[s.$parent.$index];

				if(n.status<=0){
					return false;
				}
				if(o.status==20){
					$scope.tutorname=n.tutor_name;
					$scope.amount=(parseFloat(n.first_price)+parseFloat(n.step_price*20)).toFixed(2);
					$scope.show=true;
				}
				if(o.status==30){
					
					var url=SITE_URL+"index.php?app=basetutorapp&callback=JSON_CALLBACK&act=logget&id="+o.consult_tid;
					$http.jsonp(url).success(function(data) {
						if(data.done&&data.retval==0){
							$scope.tutorname=n.tutor_name;
							$scope.show1=true;
						}
					});
				}
			}
			catch (e) {
				showcatch(e);
			}
		}

		$scope.ordersn="";
		$scope.showfinish=false;
		$scope.cid=0;
		//完成
		$scope.finish=function(o,e){
			try{
				e.stopPropagation();
				$scope.cid=o.consult_tid;
				if(o.status!=30){
					return false;
				}

				$scope.tutorname=o.tutor_name;
				$scope.ordersn=o.order_sn;
				$scope.showfinish=true;
			}
			catch (e) {
				showcatch(e);
			}
		}

		//关闭完成
		$scope.closefinish=function(){
			try{
				$scope.tutorname="";
				$scope.ordersn="";
				$scope.showfinish=false;
				$scope.cid=0;
			}
			catch (e) {
				showcatch(e);
			}
		}

		//取消订单
		$scope.finishdone=function(){
			try{
				var url=SITE_URL+"index.php?app=basetutorapp&callback=JSON_CALLBACK&act=finish&id="+$scope.cid;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						showcatch(data.msg);
						for(var i=0;i<$scope.order.length;i++){
							if($scope.order[i].consult_tid==$scope.cid){
								$scope.order.splice(i,1);
							}
						}
						
						$rootScope.orthermsg(data.retval.push);

						$rootScope.go("order2");
					}
					$scope.tutorname="";
					$scope.ordersn="";
					$scope.showfinish=false;
					$scope.cid=0;
				});

			}
			catch (e) {
				showcatch(e);
			}
		}

		//关闭支付
		$scope.close=function(){
			try{
				$scope.tutorname="";
				$scope.amount=0;
				$scope.show=false;
				$scope.s={};
			}
			catch (e) {
				showcatch(e);
			}
		}

		//关闭取消
		$scope.closecancel=function(){
			try{
				$scope.tutorname="";
				$scope.show1=false;
				$scope.s={};
			}
			catch (e) {
				showcatch(e);
			}
		}

		//取消订单
		$scope.cancel=function(){
			try{
				var n=$scope.s.t;
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=cancel&id="+n.consult_tid;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						showcatch(data.msg);
						$scope.order.splice($scope.s.$parent.$index,1);
						$rootScope.orthermsg(data.retval.push);
						
						$rootScope.go("order3");
					}
					$scope.tutorname="";
					$scope.show1=false;
				});

			}
			catch (e) {
				showcatch(e);
			}
		}
		
		
		//展开点击
		$scope.zanclick=function(s){
			
			$scope.order[s.$index].hh=Math.abs(Math.abs($scope.order[s.$index].hh)-1);
		}
		

		//支付跳转
		$scope.payturn=function(){
			try{
				var n=$scope.s.t;
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=pay&id="+n.consult_tid+"&t="+n.tutor_tid;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						showcatch(data.msg);
						$scope.order[$scope.s.$parent.$index]=data.retval;
						$rootScope.orthermsg(data.retval.push);

						if($scope.order[$scope.s.$parent.$index].timeready!=undefined &&$scope.order[$scope.s.$parent.$index].timeready>0){
							$scope.djs($scope.s.$parent.$index);						
						}

					}else if(data.msg=="您的余额不足"){
						$rootScope.go("bag",{"id":n.consult_tid,"t":n.tutor_tid});
					}

					$scope.tutorname="";
					$scope.amount=0;
					$scope.show=false;
					$scope.s={};
				});

			}
			catch (e) {
				showcatch(e);
			}
		}

		$scope.play = function(s){
			try{
				var vp=$scope.order[s.$index].voice_path;
				if(vp!=null&&vp!=''){
					//播放音频 m 即为音频地址
					if($rootScope.media!=undefined&&$rootScope.media!=null){
						$scope.order[s.$index].playm="voice.png";
						$rootScope.media.release();
						/*
						$rootScope.media.stop();
						if(n.fromuser==$scope.m.user_id){
							$scope.queue[$scope.sindex].playm="yellow_video.png";
						}
						else{
							$scope.queue[$scope.sindex].playf="write_video.png";
						}
						console.log('replay png');
						console.log($scope.queue[$scope.sindex].playm);
						console.log($scope.queue[$scope.sindex].playf);
						$rootScope.media.src=$rootScope.site+n.msg;
						console.log('replay');
						console.log($rootScope.media.src);
						$scope.sindex=s.$index;
						if(n.fromuser==$scope.m.user_id){
							$scope.queue[$scope.sindex].playm="yellow_d.gif";
						}
						else{
							$scope.queue[$scope.sindex].playf="write_d.gif";
						}
						console.log('replay gif');
						console.log($scope.queue[$scope.sindex].playm);
						console.log($scope.queue[$scope.sindex].playf);
						$rootScope.media.play();*/
					}
					//else
					{

						$rootScope.media=audioplay($rootScope.site+vp);
						console.log('newplay');
						console.log($rootScope.media.src);
						$rootScope.media.successCallback=function(){
							$scope.order[s.$index].playm="video.png";
							console.log('successCallback png');
							console.log($scope.order[s.$index].playm);
							$scope.$apply();
						};
						$rootScope.media.errorCallback=function(err){
							$scope.order[s.$index].playm="video.png";
							console.log('successCallback png');
							console.log($scope.order[s.$index].playm);
							$scope.$apply();
						};

						$scope.order[s.$index].playm="write_d.gif";
						console.log('newplay png');
						console.log($scope.order[s.$index].playm);
						$rootScope.media.play();
					}
				}
			}
			catch(e){
				console.log('play');
				console.log(e);
			}
		}
		
		
		$scope.playeva = function(s){
			try{
				var vp=$scope.order[s.$index].evaluation_voice;
				if(vp!=null&&vp!=''){
					//播放音频 m 即为音频地址
					if($rootScope.media!=undefined&&$rootScope.media!=null){
						$scope.order[s.$index].playeva="voice.png";
						$rootScope.media.release();
						/*
						$rootScope.media.stop();
						if(n.fromuser==$scope.m.user_id){
							$scope.queue[$scope.sindex].playm="yellow_video.png";
						}
						else{
							$scope.queue[$scope.sindex].playf="write_video.png";
						}
						console.log('replay png');
						console.log($scope.queue[$scope.sindex].playm);
						console.log($scope.queue[$scope.sindex].playf);
						$rootScope.media.src=$rootScope.site+n.msg;
						console.log('replay');
						console.log($rootScope.media.src);
						$scope.sindex=s.$index;
						if(n.fromuser==$scope.m.user_id){
							$scope.queue[$scope.sindex].playm="yellow_d.gif";
						}
						else{
							$scope.queue[$scope.sindex].playf="write_d.gif";
						}
						console.log('replay gif');
						console.log($scope.queue[$scope.sindex].playm);
						console.log($scope.queue[$scope.sindex].playf);
						$rootScope.media.play();*/
					}
					//else
					{

						$rootScope.media=audioplay($rootScope.site+vp);
						console.log('newplay');
						console.log($rootScope.media.src);
						$rootScope.media.successCallback=function(){
							$scope.order[s.$index].playeva="video.png";
							console.log('successCallback png');
							console.log($scope.order[s.$index].playeva);
							$scope.$apply();
						};
						$rootScope.media.errorCallback=function(err){
							$scope.order[s.$index].playeva="video.png";
							console.log('successCallback png');
							console.log($scope.order[s.$index].playeva);
							$scope.$apply();
						};

						$scope.order[s.$index].playeva="write_d.gif";
						console.log('newplay png');
						console.log($scope.order[s.$index].playeva);
						$rootScope.media.play();
					}
				}
			}
			catch(e){
				console.log('play');
				console.log(e);
			}
		}
		
		
	}
	catch (e) {
		showcatch(e);
	}
});
UserOrder.UserOrderData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}